package com.bubbles.workspace.sync.entity.source.constraint;


import com.bubbles.engine.data.core.entity.BaseEntity;
import com.bubbles.workspace.sync.entity.source.metadata.SourceTable;
import com.bubbles.workspace.sync.key.source.constraint.SourceConstraintCheckKey;
import com.bubbles.workspace.sync.key.source.metadata.SourceTableKey;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * <p>description: 源数据源-检查约束 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-20
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true, exclude = {"sourceTable"})
@ToString(callSuper = true, exclude = {"sourceTable"})
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(schema = "sync", name = "source_constraint_check")
@IdClass(SourceConstraintCheckKey.class)
@Comment("源数据源-检查约束")
public class SourceConstraintCheck extends BaseEntity {
    
    @Id
    @Comment("源数据源-目录名称")
    private String sourceCatalogName;
    
    @Id
    @Comment("源数据源-数据库名称")
    private String sourceDatabaseName;
    
    @Id
    @Comment("源数据源-表名称")
    private String sourceTableName;
    
    @Id
    @Comment("源数据源-约束名称")
    private String sourceConstraintName;
    
    @Column(columnDefinition = "TEXT")
    @Comment("源数据源-检查条件")
    private String checkCondition;
    
    /**
     * true-完全验证
     * false-新插入或更新验证
     */
    @Comment("源数据源-完全验证")
    private Boolean validated;
    
    @Comment("源数据源-启用约束")
    private Boolean sourceTableEnabled;
    
    /**
     * 关联关系
     */
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "sourceCatalogName", referencedColumnName = "sourceCatalogName", insertable = false, updatable = false), @JoinColumn(name = "sourceDatabaseName", referencedColumnName = "sourceDatabaseName", insertable = false, updatable = false), @JoinColumn(name = "sourceTableName", referencedColumnName = "sourceTableName", insertable = false, updatable = false)})
    private SourceTable sourceTable;
    
    /**
     * 其他
     */
    
    public SourceConstraintCheckKey getKey() {
        return new SourceConstraintCheckKey()
                .setSourceCatalogName(this.sourceCatalogName)
                .setSourceDatabaseName(this.sourceDatabaseName)
                .setSourceTableName(this.sourceTableName)
                .setSourceConstraintName(this.sourceConstraintName);
    }
    
    public String getStringKey() {
        return getKey().toString();
    }
    
    public String getTableStringKey() {
        return new SourceTableKey()
                .setSourceCatalogName(this.sourceCatalogName)
                .setSourceDatabaseName(this.sourceDatabaseName)
                .setSourceTableName(this.sourceTableName)
                .toString();
    }
}